﻿// ==PREPROCESSOR==
// @import "%fb2k_path%scripts\marc2003\v2\common.js"
// ==/PREPROCESSOR==

var title_tf =  fb.TitleFormat("%title%");
var album_tf = fb.TitleFormat("$if2(%album% '['%date%']',)");
var artist_tf = fb.TitleFormat("%artist%");
var g_font_title = gdi.Font("Seattle Sans", 30, 1);
var g_font_album = gdi.Font("Seattle Sans", 24, 0);
var g_font_artist = gdi.Font("Seattle Sans", 28, 1);

var album_img = null;
var artist_img = null;
var g_timer = null;
var offset = 0;

var cycle = window.GetProperty("cycle", 200) - 200;
var limit = window.GetProperty("limit", 305) - 300;
var transition = window.GetProperty("transition", 801) - 800;
var artist_img_size = window.GetProperty("artist_img_size", 490)-250;

if(fb.IsPlaying) on_playback_new_track();

function on_playback_time(time) {
	if(cycle > 0 && arr.length > 1 && (time % cycle == 0)) on_mouse_wheel(1);
}

function on_playback_stop(reason) {
	if(album_img) album_img.Dispose();
	if(artist_img) artist_img.Dispose();
	g_metadb = album_img = artist_img = null;
	if(reason != 2) window.Repaint();
}

function on_timer(id) {
	offset--;
	if(offset == 0) {
		window.KillTimer(g_timer);
		CollectGarbage();
	}
	window.Repaint();
}

function  on_playback_new_track() {
	if(transition == 1) { 
		g_timer = window.CreateTimerInterval(1);
		offset = 30;
	}
	g_metadb = fb.GetNowPlaying();
	artist = artist_tf.EvalWithMetadb(g_metadb);
	title = title_tf.EvalWithMetadb(g_metadb);
	album = album_tf.EvalWithMetadb(g_metadb);
	artist_img = utils.GetAlbumArtEmbedded(g_metadb.rawpath, 4);
	if(!artist_img) utils.GetAlbumArtAsync(window.ID, g_metadb, 4);
}


function on_get_album_art_done(metadb, art_id, im, ip) {
	if(art_id == 4) artist_img = im;
	window.Repaint();
}

function on_size() {
	ww = window.width;
	wh = window.height;
}

function scale(gr, img, pos_x, pos_y, width, height, mth) {
	var s = mth == "max" ? Math.max(width / img.width, height / img.height) : Math.min(width / img.width, height / img.height);
	var nw = img.width * s;
	var nh = img.height * s;
	pos_x += ((width - nw) / 2);
	pos_y += ((height - nh) / 2);
	gr.DrawImage(img, pos_x, pos_y, nw, nh, 0, 0, img.width, img.height);
}

function on_paint(gr) {
	if(artist_img) {
		gr.FillGradRect(0, 0, ww, wh, 0, RGB(22, 51, 89), RGB(242, 242, 242));
        gr.FillSolidRect(ww-255, 5, artist_img_size+10, wh-10, RGBA(0,0,0,75));
        gr.FillSolidRect(20,wh-135,ww - 100,110,RGBA(0,0,0,150));
        scale(gr, artist_img, ww-artist_img_size-10, 0, artist_img_size, wh, "min");
        } else {
		gr.FillGradRect(0, 0, ww, wh, 0, RGB(22, 51, 89), RGB(242, 242, 242));
        gr.FillSolidRect(20,wh-135,ww - 100,110,RGBA(0,0,0,150));
	}
	
   if(album_img) {
		text_x = album_img_size + 30;
		scale(gr, album_img, (-offset * 20) + 30, wh-30-album_img_size, album_img_size, album_img_size, "min");
	} else {
		text_x = 30;
	}
	if(fb.IsPlaying) {
		gr.GdiDrawText(title, g_font_title, RGB(97, 185, 222), text_x, (offset * 10) + wh - 135, ww-text_x-40, wh,DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
		gr.GdiDrawText(album, g_font_album, RGB(255,255,255), text_x, (offset * 15) + wh - 95, ww-text_x-40, wh,DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
		gr.GdiDrawText(artist, g_font_artist, RGB(220,100,100), text_x, (offset * 20) + wh - 65, ww-text_x-40, wh,DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
	}
}

function on_mouse_rbtn_up(x, y) {
	var _menu = window.CreatePopupMenu();
	var _album = window.CreatePopupMenu();
	var _transition = window.CreatePopupMenu();
	var idx;
	_album.AppendMenuItem(MF_STRING, 490, "Small");
	_album.AppendMenuItem(MF_STRING, 600, "Large");
	_album.CheckMenuRadioItem(490, 600, window.GetProperty("album_img_size"));
	_transition.AppendMenuItem(MF_STRING, 800, "Off");
	_transition.AppendMenuItem(MF_STRING, 801, "On");
	_transition.CheckMenuRadioItem(800, 801, window.GetProperty("transition"));
	_menu.AppendMenuItem(MF_STRING | MF_POPUP, _album.ID, "Cover image size");
	_menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
	_menu.AppendMenuItem(MF_STRING | MF_POPUP, _transition.ID, "Transition effects");
	_menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
	//_menu.AppendMenuItem(MF_STRING, 99, "Properties");
	_menu.AppendMenuItem(MF_STRING, 100, "Configure...");
	idx = _menu.TrackPopupMenu(x, y);
	switch(idx) {
		case 490:
		case 600:
			window.SetProperty("album_img_size", idx);
			album_img_size = idx - 400;
			window.Repaint();
			break;
		case 800:
		case 801:
			window.SetProperty("transition", idx);
			transition = idx - 800;
			break;
		case 99:
			window.ShowProperties();
			break;
		case 100:
			window.ShowConfigure();
			break;
	}
	_menu.Dispose();
	_transition.Dispose();
	_album.Dispose();
	return true;
}

function RGBA(r, g, b, a) { return ((a << 24) | (r << 16) | (g << 8) | (b)); }